﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PropertyAdd : System.Web.UI.Page
{
    DataClassesDataContext dc = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Session["isValid"].ToString().Equals("true"))
                Response.Redirect("login.aspx");
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            Response.Redirect("login.aspx");
        }

        if (this.DropNomos.SelectedValue.Equals(""))
        {
            this.DropNomos.DataSource = from nomos in dc.Nomois
                                        orderby nomos.Onoma
                                        select new { Onoma = nomos.Onoma };

            this.DropNomos.DataTextField = "Onoma";
            this.DropNomos.DataValueField = "Onoma";
            this.DropNomos.DataBind();
            this.DropNomos.Items.Insert(0, new ListItem("Νομός", ""));

            this.DropDownList1.Items.Insert(0, new ListItem("Δήμος", ""));
        }         
    }


    private void populate()
    {
//        if (this.DropDownList1.SelectedValue.Equals(""))
  //      {
            this.DropDownList1.DataSource = from dimos in dc.Dimois
                                            where dimos.Nomos == this.DropNomos.SelectedValue
                                            orderby dimos.Onoma
                                            select new { Onoma = dimos.Onoma };
            this.DropDownList1.DataTextField = "Onoma";
            this.DropDownList1.DataValueField = "Onoma";
            this.DropDownList1.DataBind();
            this.DropDownList1.Items.Insert(0, new ListItem("Δήμος", ""));
//        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Property prop = new Property();

        if (Calendar1.SelectedDate.Year == 1)
            prop.SignatureDate = null;
        else
            prop.SignatureDate = Calendar1.SelectedDate;
        if (Calendar2.SelectedDate.Year == 1)
            prop.ReceivedDate = null;
        else
            prop.ReceivedDate = Calendar2.SelectedDate;
        if (!TextBox1.Text.Equals(""))
            try
            {
                prop.PropertySurface = System.Convert.ToDouble(TextBox1.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex; 
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Επιφάνεια"" μόνο με αριθμούς";
                return;
            }
        if( !DropNomos.SelectedValue.Equals("") )
            prop.PropertyNomos = DropNomos.SelectedItem.Value.ToString();
        if( !DropDownList1.SelectedValue.Equals("") )
            prop.PropertyDimos = DropDownList1.SelectedItem.Value.ToString();
        prop.PropertyArea = TextBox4.Text;
        if (!TextBox5.Text.Equals(""))
            try
            {

                prop.OSDENumber = long.Parse(TextBox5.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός ΟΣΔΕ"" μόνο με αριθμούς";
                return;
            }

        if (!TextBox6.Text.Equals(""))
            try
            {
                prop.AnadasmosNumber = long.Parse(TextBox6.Text);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός Αναδασμού"" μόνο με αριθμούς";
                return;
            }
        prop.RegistrationOffice = TextBox7.Text;
        prop.OwnerName = TextBox8.Text;
        prop.OwnerAddress = TextBox9.Text;
        prop.OwnerTF = TextBox10.Text;
        if (Calendar3.SelectedDate.Year == 1)
            prop.FirstSeedDate = null;
        else
            prop.FirstSeedDate = Calendar3.SelectedDate;

            if( !this.TextBox11.Text.Equals("") )
                try
                {
                    prop.FirstSeedSeed = double.Parse(TextBox11.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""1η Σπορά-Πρώτος Σπόρος"" μόνο με αριθμούς";
                    return;
                }

        if (Calendar4.SelectedDate.Year == 1)
            prop.SecondSeedDate = null;
        else
            prop.SecondSeedDate = Calendar4.SelectedDate;

            if( !this.TextBox12.Text.Equals("") )
                try
                {
                    prop.SecondSeedSeed = double.Parse(TextBox12.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""2η Σπορά-Σπόρος"" μόνο με αριθμούς";
                    return;
                }

        prop.Apopsigeoponou = TextBox13.Text;
        if (Calendar5.SelectedDate.Year == 1)
            prop.ApopsigeoponouDate = null;
        else
            prop.ApopsigeoponouDate = Calendar5.SelectedDate;
        prop.CreatedBy = (string)Session["username"];
        prop.LastUpdatedBy = (string)Session["username"];

        dc.Properties.InsertOnSubmit(prop);
        dc.SubmitChanges();

        HttpContext.Current.Session["message"] = "Το ακίνητο προστέθηκε επιτυχώς";
        Response.Redirect("default.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.Button1_Click(sender, e);
    }
    protected void DropNomos_SelectedIndexChanged(object sender, EventArgs e)
    {
        populate();
    }
}
